<template>
  <div>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="部门" prop="departmentId">
        <el-select v-model="ruleForm.departmentId" @change="getChuqin">
          <el-option
            v-for="item in deptsList"
            :key="item.id"
            :label="item.name || ''"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出勤时间" required>
        <el-row>
          <el-col :span="5">
            <el-form-item prop="morningStartTime">
              <el-time-select
                v-model="ruleForm.morningStartTime"
                :picker-options="{
                  start: '08:30',
                  step: '00:15',
                  end: '18:30',
                }"
              />-
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="morningEndTime">
              <el-time-select
                v-model="ruleForm.morningEndTime"
                :picker-options="{
                  start: '08:30',
                  step: '00:15',
                  end: '18:30',
                  minTime: ruleForm.morningStartTime,
                }"
              />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="afternoonStartTime">
              <el-time-select
                v-model="ruleForm.afternoonStartTime"
                :picker-options="{
                  start: '08:30',
                  step: '00:15',
                  end: '18:30',
                  minTime: ruleForm.morningEndTime,
                }"
              />-
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="afternoonEndTime">
              <el-time-select
                v-model="ruleForm.afternoonEndTime"
                :picker-options="{
                  start: '08:30',
                  step: '00:15',
                  end: '18:30',
                  minTime: ruleForm.afternoonStartTime,
                }"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-button type="primary" @click="btnOk">保存更新</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { settingChuqin, getChuqin } from '@/api/attendance'
export default {
  data() {
    return {
      ruleForm: {
        afternoonEndTime: '',
        afternoonStartTime: '',
        morningEndTime: '',
        morningStartTime: '',
        departmentId: ''
      },
      rules: {
        afternoonEndTime: [
          { required: true, message: '该项不能为空', trigger: 'change' }
        ],
        afternoonStartTime: [
          { required: true, message: '该项不能为空', trigger: 'change' }
        ],
        morningEndTime: [
          { required: true, message: '该项不能为空', trigger: 'change' }
        ],
        morningStartTime: [
          { required: true, message: '该项不能为空', trigger: 'change' }
        ],
        departmentId: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      },
      deptsList: []
    }
  },
  mounted() {
    this.getDepartments()
    this.ruleForm = {
      afternoonEndTime: '',
      afternoonStartTime: '',
      morningEndTime: '',
      morningStartTime: '',
      departmentId: ''
    }
  },
  methods: {
    async getChuqin(val) {
      console.log(val)
      const fd = new FormData()
      fd.append('departmentId', val)
      const res = await getChuqin(fd)
      this.ruleForm = res
      console.log(this.ruleForm.departmentId, this.ruleForm)
    },
    async getDepartments() {
      const { depts } = await getDepartments()
      console.log(depts)
      this.deptsList = depts
    },
    btnCancel() {
      this.ruleForm = {
        afternoonEndTime: '',
        afternoonStartTime: '',
        morningEndTime: '',
        morningStartTime: '',
        departmentId: ''
      }
      this.$refs.ruleForm.resetFields()
      this.$emit('updateShowDialog')
    },
    async btnOk() {
      await this.$refs.ruleForm.validate()
      await settingChuqin(this.ruleForm)
      this.$message.success('设置成功')
      this.$emit('updateShowDialog')
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor.el-input {
  width: 120px;
}
::v-deep label{
    font-weight: 400;
}
</style>
